from django.shortcuts import render, redirect, HttpResponse
from django.http import JsonResponse
from django.contrib.auth import authenticate, logout, login
from app.checks import check_name, CheckForm
from  PIL import Image, ImageFont, ImageDraw
import random
from app.models import User
from io import BytesIO
# Create your views here.

response_dic = {
    'status':1,
    'msg':'ok',
    'data':{}

}


def home(request):
    return render(request, 'home.html')


def register(request):
    if request.method == "GET":
        return render(request, 'register.html')
    if request.method == "POST":
        avatar = request.FILES.get('avatar', None)
        check_form = CheckForm(request.POST)
        if check_form.is_valid():
            cleaned_form = check_form.cleaned_data
            cleaned_form.pop('re_password')
            if avatar:
                cleaned_form['avatar'] = avatar
            print(cleaned_form)
            # 数据库插入数据
            user = User.objects.create_user(**cleaned_form)
            if user:
                return JsonResponse({
                    'status': 1,
                    'msg': '注册成功',
                    'data': {

                    }
                })
        return JsonResponse({
            'status': 2,
            'msg': '注册失败',
            'data': {

            }
        })


def check_username(request):
    if request.is_ajax():
        username = request.GET.get('username', None)
        msg = check_name(username, is_ajax=True)
        response_dic['msg'] = msg
        return JsonResponse(response_dic)


# def my_login(request):
#     if request.method == "GET":
#         next_url = request.GET.get('next', 0)
#         return render(request, 'login.html', locals())
#     if request.method == "POST":
#         next_url = request.GET.get('next', '/')
#         telephone = request.POST.get('telephone', None)
#         password = request.POST.get('password', None)
#         user = authenticate(telephone=telephone, password=password)
#         if user:
#             login(request, user)
#             return JsonResponse({
#                 'status': 1,
#                 'msg': "登录成功",
#                 'data': {
#                     'backurl': next_url
#                 }
#             })
#         return JsonResponse({
#             'status': 2,
#             'msg': "登录失败",
#             'data': {}
#         })


def my_logout(request):
    logout(request)
    return redirect('/')
from app.MyMiddle import User_ser
from rest_framework.response import Response
from app import models
from rest_framework.views import APIView
class my_login(APIView):
    def post(self, request):
        print('+++++++++')
        name = request.data.get('name')
        pwd = request.data.get('pwd')
        print(name,pwd)
        user_list = models.User.objects.filter(username=name, password=pwd)
        user_ser = User_ser(instance=user_list, many=True)
        response = {'statue':200,'msg':'ok'}
        response['data'] = user_ser.data
        return Response(response)



























